package com.sjy.ship.service.impl;

import java.util.List;

import com.sjy.ship.domain.Crew;
import com.sjy.ship.domain.vo.ShipWorkLineVo;
import com.sjy.ship.mapper.CrewMapper;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sjy.ship.mapper.ShipWorkLineMapper;
import com.sjy.ship.domain.ShipWorkLine;
import com.sjy.ship.service.IShipWorkLineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 船作业线工作记录Service业务层处理
 * 
 * @author lzm
 * @date 2023-06-14
 */
@Service
public class ShipWorkLineServiceImpl implements IShipWorkLineService 
{
    @Autowired
    private ShipWorkLineMapper shipWorkLineMapper;
    @Autowired
    private CrewMapper crewMapper;

    /**
     * 查询船作业线工作记录
     * 
     * @param shipWorkId 船作业线工作记录主键
     * @return 船作业线工作记录
     */
    @Override
    public ShipWorkLine selectShipWorkLineByShipWorkId(Long shipWorkId)
    {
        return shipWorkLineMapper.selectShipWorkLineByShipWorkId(shipWorkId);
    }

    /**
     * 查询船作业线工作记录列表
     * 
     * @param shipWorkLine 船作业线工作记录
     * @return 船作业线工作记录
     */
    @Override
    public List<ShipWorkLineVo> selectShipWorkLineVoList(ShipWorkLineVo shipWorkLine)
    {
        return shipWorkLineMapper.selectShipWorkLineVoList(shipWorkLine);
    }

    /**
     * 新增船作业线工作记录
     * @param shipWorkLine 船作业线工作记录
     */
    @Transactional
    @Override
    public void insertShipWorkLine(ShipWorkLine shipWorkLine)
    {
        int addCrew=1;
        int addLine = shipWorkLineMapper.insertShipWorkLine(shipWorkLine);
        if (shipWorkLine.getCrew()!=null){
            //将新增后得到的工作线id设置到船舶配置对象中
            shipWorkLine.getCrew().setShipWorkId(shipWorkLine.getShipWorkId());
            addCrew=crewMapper.insertCrew(shipWorkLine.getCrew());
        }
        Assert.isTrue(addLine+addCrew>1,"新增失败，请稍后重试");
    }

    /**
     * 修改船作业线工作记录
     * 
     * @param shipWorkLine 船作业线工作记录
     */
    @Transactional
    @Override
    public void updateShipWorkLine(ShipWorkLine shipWorkLine)
    {
        Crew crew = crewMapper.selectCrewByShipWorkId(shipWorkLine.getShipWorkId());
        int alterCrew=1;
        if (crew==null && shipWorkLine.getCrew()!=null){
            alterCrew=crewMapper.insertCrew(shipWorkLine.getCrew());
        }else if (crew!=null && shipWorkLine.getCrew()!=null){
            alterCrew=crewMapper.updateCrew(shipWorkLine.getCrew());
        }
        Assert.isTrue(shipWorkLineMapper.updateShipWorkLine(shipWorkLine)+alterCrew>1,"更新失败，请稍后重试");
    }

    /**
     * 批量删除船作业线工作记录
     * 
     * @param shipWorkIds 需要删除的船作业线工作记录主键
     * @return 结果
     */
    @Override
    public int deleteShipWorkLineByShipWorkIds(Long[] shipWorkIds)
    {
        return shipWorkLineMapper.deleteShipWorkLineByShipWorkIds(shipWorkIds);
    }

    /**
     * 删除船作业线工作记录信息
     * 
     * @param shipWorkId 船作业线工作记录主键
     * @return 结果
     */
    @Override
    public int deleteShipWorkLineByShipWorkId(Long shipWorkId)
    {
        return shipWorkLineMapper.deleteShipWorkLineByShipWorkId(shipWorkId);
    }
}
